<%@ tag pageEncoding="UTF-8"%>
<!-- Spring 的分页对象 -->
<%@ attribute name="page" type="org.springframework.data.domain.Page" required="true"%>
<!-- 分页控件显示的数量 -->
<%@ attribute name="paginationSize" type="java.lang.Integer" required="true"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<%
int current =  page.getNumber() + 1;
int begin = Math.max(1, current - paginationSize/2);
int end = Math.min(begin + (paginationSize - 1), page.getTotalPages());

request.setAttribute("current", current);
request.setAttribute("begin", begin);
request.setAttribute("end", end);
%>

<!-- 分页控件 -->
<ul class="pagination" style="margin-top: 0px;">

  <!-- 如果当前页是首页，则禁用首页与前一页的按钮 -->
  <% if (page.hasPreviousPage()){%>
    <li><a href="?page=1&sortType=${sortType}&${searchParams}">&lt;&lt;</a></li>
    <li><a href="?page=${current-1}&sortType=${sortType}&${searchParams}">&lt;</a></li>
  <%}else{%>
    <li class="disabled"><a href="#">&lt;&lt;</a></li>
    <li class="disabled"><a href="#">&lt;</a></li>
  <%} %>
  
  <c:forEach var="i" begin="${begin}" end="${end}">
    <c:choose>
      <c:when test="${i == current}">
        <li class="active"><a href="?page=${i}&sortType=${sortType}&${searchParams}">${i}</a></li>
      </c:when>
      <c:otherwise>
        <li><a href="?page=${i}&sortType=${sortType}&${searchParams}">${i}</a></li>
      </c:otherwise>
    </c:choose>
  </c:forEach>
	
  <!-- 如果当前页是末页，则禁用首页与前一页的按钮 -->	  
  <% if (page.hasNextPage()){%>
    <li><a href="?page=${current+1}&sortType=${sortType}&${searchParams}">&gt;</a></li>
    <li><a href="?page=${page.totalPages}&sortType=${sortType}&${searchParams}">&gt;&gt;</a></li>
  <%}else{%>
    <li class="disabled"><a href="#">&gt;</a></li>
    <li class="disabled"><a href="#">&gt;&gt;</a></li>
  <%} %>
  
</ul>
